/*
*    Author : huyn
*    2017/05/15
*    version 0.0.9
*    配置文件操作实现方法
*/

#include "htsc_grpc_common_utils.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "time.h"
#include "grpc/support/time.h"
#include "htsc_grpc_utils.h"
#include "htsc_grpc_conf.h"
#include "htsc_common/htsc_grpc_properties_tools.h"
//#include <gpr_types.h>

#include "uuid4gen.h"

//#ifdef WIN64
//#include "uuid_win\uuid.h"
//#else
//#include "uuid_linux\htsc_grpc_common_uuid_extend.h"
//#endif
 

/*
* 本功能windows平台和linux平台实现由差异
*/
void htsc_grpc_uuid(char *my_uuid) {
	//size_t size = 37 * sizeof(char);
	//my_uuid = (char *)malloc(size);
	////for (int i = 0;i < gencount;i++) {
 //   memset(my_uuid, 0, size);
	//gen_uuid(my_uuid);
	//// printf("seqno: %d , my_uuid: %s\n",i, my_uuid);
 //   //FREE_PTR(my_uuid);
	////}
	////FREE_PTR(my_uuid);
 
	uuid4gen(my_uuid);
}



//把整数转为对应的字符串
void htsc_grpc_int_to_char(int inval,char * str) {
	sprintf(str, "%d", inval);
	//return str;
}


//用于解析zookeeper:///前缀的服务名
//例如zookeeper:///com.htsc.aaaa.bbb.cccImpl
int htsc_grpc_getservicename_by_target(char * target) {
	if (target == NULL) {
		return -1;
	}
	int targetlen= strlen(target);
	int schemelen = strlen(HTSC_GRPC_COMMON_ZK_SCHEME);
	if (targetlen <= schemelen) {
		return -1;
	}
	char * servicename = strrchr(target, '/');
	if (servicename == NULL) {
		return -1;
	} 
	return targetlen - strlen(servicename) +1;
}


//解析fullmethod变量内包含的服务名
//fullmethod格式为：  /com.aaa.bbb.ccc/methodname
void htsc_grpc_getserveicename_by_fullmethod(const char* fullmethod,char* servicename){
	if (fullmethod == NULL) {
		return ;
	}	
	char *strmethod=(char*)malloc(200);
	memset(strmethod, 0, 200);
	strcpy(strmethod, fullmethod);
	char *temp = strchr(strmethod, '/');
	if ((strmethod - temp) == 0) {
		temp = strchr(temp+1, '/');
		strmethod++;
	}
	int len = temp - strmethod;
	if (len > 0) {
		memcpy(servicename, strmethod, (temp - strmethod));
	}
	//FREE_PTR(strmethod);
	//FREE_PTR(temp);
	return ;
}

//解析fullmethod变量内包含的服务名
//fullmethod格式为：  /com.aaa.bbb.ccc/methodname
char * htsc_grpc_getserveice_by_fullmethod(const char* fullmethod) {
	if (fullmethod == NULL) {
		return NULL;
	}
	size_t len = strlen(fullmethod);
	if (len <= 0) {
		return NULL;
	}
	size_t size = sizeof(char)*len + sizeof(char);
	char *strmethod = (char*)malloc(size);
	memset(strmethod, 0, size);
	memcpy(strmethod, fullmethod, len);

	char *temp = strchr(strmethod, '/');
	if ((strmethod - temp) == 0) {
		temp = strchr(temp + 1, '/');
		strmethod++;
	}
	len = temp - strmethod;
	if (len <= 0) {
		return NULL;
	}
	char* servicename =(char*)malloc(len*sizeof(char) + sizeof(char));
	memset(servicename, 0, len * sizeof(char) + sizeof(char));
	memcpy(servicename, strmethod, len);
	temp = NULL;
	strmethod--; //恢复到头位置
	FREE_PTR(strmethod);
	return servicename;
}


//解析fullmethod变量内包含的服务名
//fullmethod格式为：  /com.aaa.bbb.ccc/methodname
char * htsc_grpc_getmethodname_by_fullmethod(const char* fullmethod) {	
	if (fullmethod == NULL) {
		return NULL;
	}
	size_t len = strlen(fullmethod);
	if (len <= 0) {
		return NULL;
	}
	size_t size = sizeof(char)*len + sizeof(char);
	char *strmethod = (char*)malloc(size);
	memset(strmethod, 0, size);
	memcpy(strmethod, fullmethod,len);
	char *temp = strrchr(strmethod, '/');
	if ((temp - strmethod ) <= 0) {
		free(strmethod);
		return NULL;
	}
	temp++;
	len = len- (temp - strmethod);
	if (len <= 0) {
		return NULL;
	}
	size = sizeof(char) * len;
	char *methodname = (char*)malloc(size + sizeof(char));
	memset(methodname, 0, size+sizeof(char));
	memcpy(methodname, temp,len);
	temp = NULL;
	FREE_PTR(strmethod);
	return methodname;
}

int htsc_grpc_router_free(htsc_grpc_router_t **router) {
	htsc_grpc_router_t *routertemp = *router;
	if (!routertemp)
	{
		return 0;
	}
	FREE_PTR(routertemp->url);
	FREE_PTR(routertemp->servicename);
	FREE_PTR(routertemp->host);
	FREE_PTR(routertemp);
	return 0;
}

//获取consumer端所需要的provider版本
//获取内容不需要释放
static char g_consumer_service_version[100] = { 0 };
char* htsc_consumer_service_version() {
	if (g_consumer_service_version[0] != '\0')
	{
		return g_consumer_service_version;
	}
	if (0 == htsc_grpc_properties_get_value(HTSC_GRPC_CONF_CONSUMER_SERVICE_VERSION, NULL, g_consumer_service_version)) {
		return g_consumer_service_version;
	}
	return NULL;
}

//获取provider端提供服务版本版本
//获取内容不需要释放
static char g_provider_service_version[500] = { 0 };
char* htsc_provider_service_version() {
	if (g_provider_service_version[0] != '\0')
	{
		return g_provider_service_version;
	}
	if (0 == htsc_grpc_properties_get_value(HTSC_GRPC_CONF_PROVIDER_VERSION, NULL, g_provider_service_version)) {
		return g_provider_service_version;
	}
	return NULL;
}

//获取provider端提供服务版本版本
//获取内容不需要释放
static char g_provider_service_group[500] = { 0 };
char* htsc_provider_service_group() {
	if (g_provider_service_version[0] != '\0')
	{
		return g_provider_service_group;
	}
	if (0 == htsc_grpc_properties_get_value(HTSC_GRPC_CONF_PROVIDER_GROUP, NULL, g_provider_service_group)) {
		return g_provider_service_group;
	}
	return NULL;
}


//校验正整数
//如果是正整数返回1,否则返回0
int htsc_grpc_common_utils_isdigit(char *num)
{
	int i;
	for (i = 0;num[i];i++)
	{  
		if (i == 0 && num[i] == '0') {
			return 0;
		}
		if (num[i] > '9' || num[i] < '0') //只要有非数字，就返回错误
		{
			return 0;
		}
	}
	//if (i>4) return 0;  ////都是数字，但长度超过4位，返回错误
	return 1;
}

long htsc_grpc_common_utils_strtolong(char * strdigit) {
	return atol(strdigit);
}

//获取consumer端所需要的provider版本
//获取内容不需要释放
static char g_htsc_grpc_version[100] = { 0 };
char* htsc_grpc_version() {
	if (g_htsc_grpc_version[0] != '\0')
	{
		return g_htsc_grpc_version;
	}
	if (0 == htsc_grpc_properties_get_value(HTSC_GRPC_CONF_COMMON_GRPC, NULL, g_htsc_grpc_version)) {
		return g_htsc_grpc_version;
	}
	return NULL;
}